<p>&nbsp;</p>
<table align="center" width="100%"><tr><td align="center" width="100%">
	<div id="cpanel" align="center" style="width:580px;">
<?php
/**
* Author: D.Wassim
* Contact: wdammak@talend.com
**/ 
//require_once "helper.php";
//require_once "languages/default.php";

function php2js( $php_array, $js_array_name ) {
	// contrle des parametres d'entre
	if( !is_array( $php_array ) ) {
		//trigger_error( "php2js() => 'array' attendu en parametre 1, \n'".gettype($array)."' fourni !?!");
		return false;
	}
	if( !is_string( $js_array_name ) ) {
		//trigger_error( "php2js() => 'string' attendu en parametre 2, \n'".gettype($array)."' fourni !?!");
		return false;
	}

	// Cration du tableau en JS
	$script_js = "var $js_array_name = new Array();\n";
	
	// on rempli le tableau JS  partir des valeurs de son homologue PHP
	foreach( $php_array as $key => $value ) {
		if(!is_array($value)){		
			$value=str_replace("\r\n","",$value);
			$value=str_replace("\n\r","",$value);
			$value=str_replace("\n","",$value);
			$value=str_replace("\r","",$value);			
			$value=str_replace("","\\n",$value);
			$value=str_replace('&#x',"\\u",$value);
			$value=str_replace('%u',"\\u",$value);
		}
		// pouf, on tombe sur une dimension supplementaire
		if( is_array($value) ) {
			// On va demander la cration d'un tableau JS temporaire
			$temp = uniqid('temp_'); // on lui choisi un nom bien barbare
			$t = php2js( $value, $temp ); // et on creer le script JS
			// En cas d'erreur, remonter l'info aux rcursions suprieures
			if( $t===false ) return false;

			// Ajout du script de cration du tableau JS temporaire
			$script_js.= $t;
			// puis on applique ce tableau temporaire  celui en cours de construction
			$script_js.= "{$js_array_name}['{$key}'] = {$temp};\n";
		}
		
		// Si la clef est un entier, pas de guillemets
		elseif( is_int($key) )  $script_js.= "{$js_array_name}[{$key}] = '{$value}';\n";
		
		// sinon avec les guillemets
		else 
			$script_js.= "{$js_array_name}['{$key}'] = '{$value}';\n";
	}

	// Et retourn le script JS
	return $script_js;
}


//Connexion  la base de donnes
$connect = mysql_connect(DB_SERVEUR,DB_USER,DB_PASS);
mysql_select_db(DB_BDD) or die("erreur de connexion a la base de donnees");
mysql_query('SET NAMES "utf8"');

function getButtons(){
	return '<input type="submit" name="create" value="Create" />
		  <input type="submit" name="edit" value="Edit" />
		  <input type="submit" name="enable" value="Enable" />
		  <input type="submit" name="disable" value="Disable" />
		  <input type="submit" name="delete" value="Delete" />';
}

$action=$_GET['action'];

//Releases
if($action=="releases"){
	//html
	$message="";
	if(isset($_POST['create'])){
		if($_POST['name']!=''){
			/*$create="INSERT INTO releases(name,date,id_projet,status,path,include_files_pattern,include_folder_begin_by,exclude_folder_end_by,exclude_folder_begin_by) VALUES('".$_POST['name']."','".$_POST['date']."','".$_POST['id_projet']."','0','".$_POST['path']."','".$_POST['include_files_pattern']."','".$_POST['include_folder_begin_by']."','".$_POST['exclude_folder_end_by']."','".$_POST['exclude_folder_begin_by']."')";*/
			
			$create="INSERT INTO releases(name,date,id_projet,status) VALUES('".$_POST['name']."','".$_POST['date']."','".$_POST['id_projet']."','1')";
			$result=mysql_query($create);
			$message= "Release Created"; 
		}else{
			$message= "Please fill project name";
		}
	}
	elseif(isset($_POST['edit'])){
		if($_POST['name']!=''){
			$edit="UPDATE releases SET name ='".$_POST['name']."' WHERE id_release='".$_POST['selected']."'";
			$result=mysql_query($edit);
			$message= "Release name Edited"; 
		}
		if($_POST['date']!=''){
			$edit="UPDATE releases SET date ='".$_POST['date']."' WHERE id_release='".$_POST['selected']."'";
			$result=mysql_query($edit);
			$message= "Release date Edited"; 
		}
		if($_POST['id_projet']!=''){
			$edit="UPDATE releases SET id_projet ='".$_POST['id_projet']."' WHERE id_release='".$_POST['selected']."'";
			$result=mysql_query($edit);
			$message= "Sssign Release to project Edited"; 
		}
	}
	elseif(isset($_POST['enable'])){ 
		$enable="UPDATE releases SET status='1' WHERE id_release='".$_POST['selected']."'";
		$result=mysql_query($enable);
		$message= "Release Enaled"; 
	}
	elseif(isset($_POST['disable'])){
		$disabled="UPDATE releases SET status='0' WHERE id_release='".$_POST['selected']."'";
		$result=mysql_query($disabled);
		$message= "Release Disabled"; 
	}
	elseif(isset($_POST['delete'])){ 
		$delete="DELETE FROM releases WHERE id_release='".$_POST['selected']."'";
		$result=mysql_query($delete);
		$message= "Release Deleted"; 
	}elseif(isset($_POST['close'])){ 
		$delete="UPDATE releases SET closed='1' WHERE id_release='".$_POST['selected']."'";
		$result=mysql_query($delete);
		$message= "Release Closed"; 
	}
			
	$list_releases="Select r.*, p.name as name_projet from releases r, projets p WHERE r.id_projet=p.id_projet ORDER BY id_release DESC";
	$result=mysql_query($list_releases);
	$datas_releases=array();
	while ($row = mysql_fetch_assoc($result)) {
		array_push(
			$datas_releases,
			array(
				'id_release' => $row['id_release'],
				'name' =>$row['name'],
				'id_projet' => $row['id_projet'],
				'name_projet' =>$row['name_projet'],
				'date' => $row['date'],
				'status' => $row['status'],
			)
		);
	}	
	
	$list_projets="SELECT * FROM projets WHERE status=1";
	$result=mysql_query($list_projets);
	//$datas_groups=array('id_group'=>'','name'=>'');
	$datas_projets=array();
	while ($row = mysql_fetch_assoc($result)) {
		array_push(
			$datas_projets,
			array(
				'id_projet' => $row['id_projet'],
				'name' => $row['name'],
			)
		);
	}	
	
	//Show Releases list
	echo '<br/><form action="?action=releases" method="post" name="task">
				<input type="hidden" name="date" value="'.date("Y-m-d").'"/>
	<br/><table>
			<tr>
				<td>List Releases:</td>
				<td>
					<select name="selected">';
					foreach($datas_releases as $item){
						echo '<option value="'.$item['id_release'].'" ';
						echo ($_POST['selected']==$item['id_release'])? 'selected="selected"':''; 
						echo '>'.$item['name']." - ".$item['date']." - ".$item['name_projet'];
						echo ($item['status']==0)?" (Disabled)":'';
						echo '</option>';
					}
					//Show button	
					echo '</select>
				</td>
			</tr>
			<tr>
				<td>Release name:</td>
				<td><input type="text" name="name"/></td>
			</tr>
			<tr>
				<td>Assign to Project:&nbsp;</td>
				<td><select name="id_projet"><option></option>';
				foreach($datas_projets as $item)
					echo '<option value="'.$item['id_projet'].'">'.$item['name'].'</option>'; 
				echo '</select></td>
			</tr>
		</table>				
		<br/>'.getButtons().' <input type="submit" name="close" value="Close" />				
		</form><br/>';
	echo "<p class=\"message\">$message</p>";
	
				/*<tr>
				<td>Date:</td>
				<td><input type="hidden" name="date" value="'.date("Y-m-d").'"/></td>
			</tr>
			<tr>
				<td>Absolute path:</td>
				<td><input type="text" name="path" value="/home/www/talendforge.org/babili/admin/" size="40" /></td>
			</tr>
			<tr>
				<td>Patterns files:</td>
				<td><input type="text" name="include_files_pattern" value="*messages.properties,*build.properties,*plugin.properties" size="40" /></td>
			</tr>
			<tr>
				<td>Include Folders Begin By:</td>
				<td><input type="text" name="include_folder_begin_by" value="org.talend." size="40" /></td>
			</tr>
			<tr>
				<td>Exclude Folders End By:</td>
				<td><input type="text" name="exclude_folder_end_by" value=".nl" size="40" /></td>
			</tr>
			<tr>
				<td>Exclude Folder Begin By</td>
				<td><input type="text" name="exclude_folder_begin_by" value="org.talend.rcp,org.talend.libraries" size="40" /></td>
			</tr>*/
}

//Langues
elseif($action=="langues"){
	
	//html
	$message="";
	if(isset($_POST['create'])){
		if($_POST['name']!=''){
			$create="INSERT INTO languages(name,short_name,flag,status,export_short_name) VALUES('".$_POST['name']."','".$_POST['short_name']."','".$_POST['flag']."','0','".strtolower($_POST['short_name'])."')";
			$result=mysql_query($create);
			
			$id_lang=$_POST['selected']=mysql_insert_id();

			set_time_limit(0);
			//Duplicate keys
			$result=mysql_query("SELECT id_key FROM translation where id_lang=1 GROUP BY id_key");
			$datas_keys=array();
			while ($row = mysql_fetch_assoc($result)) {
				array_push(
					$datas_keys,
					array(
						'id_key' => $row['id_key'],
						'id_lang' => $id_lang,
					)
				);
			}
			
			foreach($datas_keys as $item){
				$result=mysql_query("INSERT INTO translation (id_key, id_lang, translation, id_status, id_user_trans, id_user_validate, id_editor, date) 
											VALUES ('".$item['id_key']."','".$item['id_lang']."','',0,0,0,0,'0000-00-00 00:00:00');");
			}
			$message= "Langue Created"; 
		}else{
			$message= "Please fill Langue name";
		}
	}
	elseif(isset($_POST['edit'])){
		if($_POST['name']!=''){
			$edit="UPDATE languages SET name ='".$_POST['name']."' WHERE id_lang='".$_POST['selected']."'";
			$result=mysql_query($edit);
			$message= "Langue name Edited"; 
		}
		if($_POST['short_name']!=''){
			$edit="UPDATE languages SET short_name ='".$_POST['short_name']."' WHERE id_lang='".$_POST['selected']."'";
			$result=mysql_query($edit);
			$message= "Langue short name Edited"; 
		}
		if($_POST['flag']!=''){
			$edit="UPDATE languages SET flag ='".$_POST['flag']."' WHERE id_lang='".$_POST['selected']."'";
			$result=mysql_query($edit);
			$message= "Langue flag Edited"; 
		}
	}
	elseif(isset($_POST['enable'])){ 
		$enable="UPDATE languages SET status='1' WHERE id_lang='".$_POST['selected']."'";
		$result=mysql_query($enable);
		$message= "Langue Enaled"; 
	}
	elseif(isset($_POST['disable'])){
		$disabled="UPDATE languages SET status='0' WHERE id_lang='".$_POST['selected']."'";
		$result=mysql_query($disabled);
		$message= "Langue Disabled"; 
	}
	elseif(isset($_POST['delete'])){ 
		$delete="DELETE FROM languages WHERE id_lang='".$_POST['selected']."'";
		$result=mysql_query($delete);
		$message= "Langue Deleted"; 
	}
			
	$list_lang="SELECT id_lang, name, short_name, flag, status FROM languages ORDER BY id_lang ASC";
	$result=mysql_query($list_lang);
	$datas_lang=array();
	while ($row = mysql_fetch_assoc($result)) {
		array_push(
			$datas_lang,
			array(
				'id_lang' => $row['id_lang'],
				'name' =>$row['name'],				
				'short_name' =>$row['short_name'],
				'flag' =>$row['flag'],
				'status' =>$row['status'],
			)
		);
	}
	
	//Show project list
	echo '<br/><form action="?action=langues" method="post" name="task">
					List Languages: <select name="selected">';
		foreach($datas_lang as $item){
			echo '<option value="'.$item['id_lang'].'" style="background:url('.BABILI_URL."/".$item['flag'].') no-repeat right;" ';
			echo ($_POST['selected']==$item['id_lang'])? 'selected="selected"':''; 
			echo '>'.$item['name']."--".$item['short_name']."&nbsp;";
			echo ($item['status']==0)?" (Disabled)":'';
			echo '</option>';
		}
	//Show button	
	echo '</select><br/><br/><table><tr><td>
				Langue name:</td><td><input type="text" name="name"/>&nbsp;(Ex. English)</td></tr><td>
				Langue short name:</td><td><input type="text" name="short_name" />&nbsp;(Ex. US)</td></tr><td>
				Flag url:</td><td><input type="text" name="flag" />&nbsp;(Ex. img/us.gif)</td></tr></table>
				
				<br/>'.getButtons().'
						
		</form><br/>';
	echo "<p class=\"message\">$message</p>";
	
}
//Projects
elseif($action=="projects"){
	
		//html
	$message="";
	if(isset($_POST['create'])){
		if($_POST['name']!='' || $_POST['projet_code']!=''){
			$create="INSERT INTO projets(name,projet_code,status) VALUES('".$_POST['name']."','".$_POST['projet_code']."','0')";
			$result=mysql_query($create);
			$_POST['selected']=mysql_insert_id();
		}else{
			$message= "Please fill Project name and Project short name";
		}
	}
	elseif(isset($_POST['edit'])){
		if($_POST['name']!=''){
			$edit="UPDATE projets SET name ='".$_POST['name']."' WHERE id_projet='".$_POST['selected']."'";
			$result=mysql_query($edit);
			$message= "Project name Edited"; 
		}
		if($_POST['projet_code']!=''){
			$edit="UPDATE projets SET projet_code ='".$_POST['projet_code']."' WHERE id_projet='".$_POST['selected']."'";
			$result=mysql_query($edit);
			$message= "Project short name Edited"; 
		}
	}
	elseif(isset($_POST['enable'])){ 
		$enable="UPDATE projets SET status='1' WHERE id_projet='".$_POST['selected']."'";
		$result=mysql_query($enable);
		$message= "Project Enabled"; 
	}
	elseif(isset($_POST['disable'])){
		$disabled="UPDATE projets SET status='0' WHERE id_projet='".$_POST['selected']."'";
		$result=mysql_query($disabled);
		$message= "Project Disabled"; 
	}
	elseif(isset($_POST['delete'])){ 
		$delete="DELETE FROM projets WHERE id_projet='".$_POST['selected']."'";
		$result=mysql_query($delete);
		
		/**
		$id_lang=mysql_insert_id();
		set_time_limit(0);
			//Duplicate keys
			$result=mysql_query("SELECT id_key FROM translation where id_lang=1 GROUP BY id_key");
			$datas_keys=array();
			while ($row = mysql_fetch_assoc($result)) {
				array_push(
					$datas_keys,
					array(
						'id_key' => $row['id_key'],
						'id_lang' => $id_lang,
					)
				);
			}
			
			foreach($datas_keys as $item){
				$result=mysql_query("INSERT INTO translation (id_key, id_lang, translation, id_status, id_user_trans, id_user_validate, id_editor, date) 
											VALUES ('".$item['id_key']."','".$item['id_lang']."','',0,0,0,0,'0000-00-00 00:00:00');");
			}
			$message= "Project Created"; 
		**/
		
		
		
		$message= "Project Deleted"; 
	}
			
	$list_lang="SELECT id_projet, name, projet_code, status FROM projets ORDER BY id_projet ASC";
	$result=mysql_query($list_lang);
	$datas_lang=array();
	while ($row = mysql_fetch_assoc($result)) {
		array_push(
			$datas_lang,
			array(
				'id_projet' => $row['id_projet'],
				'name' =>$row['name'],				
				'projet_code' =>$row['projet_code'],
				'status' =>$row['status'],
			)
		);
	}
	
	//Show project list
	echo '<br/><form action="?action=projects" method="post" name="task">
					List Projects: <select name="selected">';
		foreach($datas_lang as $item){
			echo '<option value="'.$item['id_projet'].'" ';
			echo ($_POST['selected']==$item['id_projet'])? 'selected="selected"':''; 
			echo '>'.$item['name']."--".$item['projet_code']."&nbsp;";
			echo ($item['status']==0)?" (Disabled)":'';
			echo '</option>';
		}
	//Show button	
	echo '</select><br/><br/><table><tr><td>
				Project name:</td><td><input type="text" name="name"/>&nbsp;(Ex. English)</td></tr><td>
				Project short name:</td><td><input type="text" name="projet_code" />&nbsp;(Ex. TOS, TOP, MDM)</td></tr><td>
				</tr></table>
				
				<br/>'.getButtons().'
						
		</form><br/>';
	echo "<p class=\"message\">$message</p>";

}

else{
?>
	<div style="float: left;">
		<div class="icon">
			<a href="?action=langues">
				<img src="img/langues_manager.png" alt="Langues Manager" />
				<span>Langues Manager</span></a>
		</div>
	</div>
	
	<div style="float: left;">
		<div class="icon">
			<a href="?action=releases">
				<img src="img/releases.png" alt="Releases Manager" />
				<span>Releases Manager</span></a>
		</div>
	</div>
	
	<div style="float: left;">
		<div class="icon">
			<a href="releases.php">
				<img src="img/licence.png" alt="Import New Files/Keys" />
				<span>Import New Files/Keys (Loading releases)</span></a>
		</div>
	</div>

	<div style="float: left;">
		<div class="icon">
			<a href="?action=projects">
				<img src="img/licence.png" alt="Manage Projects" />
				<span>Manage Projects</span></a>
		</div>
	</div>

	<div style="float: left;">
		<div class="icon">
			<a href="export_babili/">
				<img src="img/generate.png" alt="Export files proporties" />
				<span>Export Release</span></a>
		</div>
	</div>

	<div style="float: left;">
		<div class="icon">
			<a href="synchronization.php">
				<img src="img/migration.png" alt="Synchronization Translation" />
				<span>Synchronization Translation</span></a>
		</div>
	</div>	
	
	<div style="float: left;">
		<div class="icon">
			<a href="../">
				<img src="img/cancel.png" alt="Exit" />
				<span>Exit</span></a>
		</div>
	</div>
<?php
}

?>
	</div>
</td></tr></table>
<br />